package com.le.dub.conf;

import cn.hutool.core.util.ObjectUtil;
import com.le.dub.common.BusinessRuntimeException;
import com.le.dub.common.Response;
import com.le.dub.common.ResultCode;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @description:统一处理异常
 * @author: guole
 * @date: 2020/4/9
 */
@ControllerAdvice
public class GlobalExceptionResolver {
    /**
     * 处理java异常
     *
     * @param e 异常
     * @return json结果
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Response handleException(Exception e) {
        // 打印异常堆栈信息
        e.printStackTrace();
        return Response.error(ResultCode.UNKNOWN_ERROR);
    }

    /**
     * 处理业务异常
     *
     * @param e 业务异常
     * @return json结果
     */
    @ExceptionHandler(BusinessRuntimeException.class)
    @ResponseBody
    public Response handleOpdRuntimeException(BusinessRuntimeException e) {
        // 打印异常堆栈信息
        e.printStackTrace();
        if(ObjectUtil.isNotNull(e.getResultCode())){
            return Response.error(e.getResultCode());

        }else {
            return Response.error(e.getCode(),e.getMsg());

        }
    }

}
